package pearson.automation.forms;

import org.openqa.selenium.By;
import org.testng.Assert;

import pearson.automation.forms.registration.AccountPage;

import framework.elements.Button;
import framework.elements.ElementList;
import framework.elements.Label;
import framework.elements.Link;

public class BaseFormWithSidebarsAndSearch extends BaseFormWithSidebars implements Search {

    /* Account management */
    private Button account = new Button(By.linkText("Account"), "Account");
    private Button myAccount = new Button(By.linkText("My Account"), "My Account");
    private Button logout = new Button(By.linkText("Sign Out"), "Logout");
    
    private Label userName = new Label(By.className("user-name"), "User Name");

    /* RightSide filters */
    public enum RightFilter {
    AUTHOR, CONTENT_TYPE, SUBJECT, COURSE_GROUP, COURSE, INSTITUTION
    };

    public final ElementList author = new ElementList(By
        .xpath("//div[@class='filter-list']/h3[text()='Author']/../ul/li/a/span[@class='filter-title']"), "Author",
        "//div[@class='filter-list']/h3[text()='Author']/..//a/span[contains(.,'%1$s')]");
    public final Button authorSeeMore = new Button(By
            .xpath("//div[@class='filter-list']/h3[text()='Author']/../ul/li[@class='see-all']"), "Author - See More");

    public final ElementList contentType = new ElementList(By
        .xpath("//div[@class='filter-list']/h3[text()='Content Type']/../ul/li/a/span[@class='filter-title']"),
        "Content Type", "//div[@class='filter-list']/h3[text()='Content Type']/..//a/span[contains(.,'%1$s')]");
    public final Button contentTypeSeeMore = new Button(By
            .xpath("//div[@class='filter-list']/h3[text()='Content Type']/../ul/li[@class='see-all']"), "Content Type - See More");

    public final ElementList subject = new ElementList(By
        .xpath("//div[@class='filter-list']/h3[text()='Subject']/../ul/li/a/span[@class='filter-title']"),
        "Subject", "//div[@class='filter-list']/h3[text()='Subject']/..//a/span[contains(.,'%1$s')]");
    public final Button subjectSeeMore = new Button(By
            .xpath("//div[@class='filter-list']/h3[text()='Subject']/../ul/li[@class='see-all']"), "Subject - See More");

    public final ElementList courseGroup = new ElementList(By
        .xpath("//div[@class='filter-list']/h3[text()='Course Group']/../ul/li/a/span[@class='filter-title']"),
        "Course Group", "//div[@class='filter-list']/h3[text()='Course Group']/..//a/span[contains(.,'%1$s')]");
    public final Button courseGroupSeeMore = new Button(By
            .xpath("//div[@class='filter-list']/h3[text()='Course Group']/../ul/li[@class='see-all']"), "Course Group - See More");

    public final ElementList course = new ElementList(By
        .xpath("//div[@class='filter-list']/h3[text()='Course']/../ul/li/a/span[@class='filter-title']"), "Course",
        "//div[@class='filter-list']/h3[text()='Course']/..//a/span[contains(.,'%1$s')]");
    public final Button courseSeeMore = new Button(By
            .xpath("//div[@class='filter-list']/h3[text()='Course']/../ul/li[@class='see-all']"), "Course - See More");

    public final ElementList institution = new ElementList(By
        .xpath("//div[@class='filter-list']/h3[text()='Institution']/../ul/li/a/span[@class='filter-title']"),
        "Institution", "//div[@class='filter-list']/h3[text()='Institution']/..//a/span[contains(.,'%1$s')]");
    public final Button institutionSeeMore = new Button(By
            .xpath("//div[@class='filter-list']/h3[text()='Institution']/../ul/li[@class='see-all']"), "Institution - See More");
    
    private Link resetFilters = new Link(By.linkText("Reset filters"), "Reset filters");

    public Button searchAdvanced = new Button(By.id("advanced-search-button"), "Advanced Search");

    public BaseFormWithSidebarsAndSearch(By titleLocator, String title) {
    super(titleLocator, title);
    }

    public SearchResultsForm search(String keyword) {
    searchFilter.setText(keyword);
    searchSimple.clickAndWait();
    return new SearchResultsForm();
    }

    public SearchResultsForm resetFilters() {
    resetFilters.clickAndWait();
    return new SearchResultsForm();
    }

    public StartForm clickLogout() {
    account.click();
    logout.clickAndWait();
    return new StartForm();
    }

    public AccountPage clickMyAccount() {
    account.click();
    myAccount.clickAndWaitForNewWindow();
    return new AccountPage();
    }
    
    public String getUserName() {
    account.click();
    String username = userName.getText();
    myLibrary.click();
    return username;
    }

    public SearchResultsForm selectFilterCountChecking(
            RightFilter filter,
            boolean checking) {
        info("Filtering by " + filter.toString().toLowerCase());
        ElementList filterList = null;
        switch (filter) {
        case AUTHOR:
            filterList = author;
            break;
    
        case CONTENT_TYPE:
            filterList = contentType;
            break;
    
        case COURSE:
            filterList = course;
            break;
    
        case COURSE_GROUP:
            filterList = courseGroup;
            break;
    
        case INSTITUTION:
            filterList = institution;
            break;
    
        case SUBJECT:
            filterList = subject;
            break;
        }
        SearchResultsForm res = null;
        if (filterList.isPresent()) {
            filterList.selectRandomElement(5);
            res = new SearchResultsForm();
            if (checking) {
                Assert.assertEquals(filterList.getListElements().size(), 1,
                        "Filter was not applied");
            }
        }
        return res;
    }

    public SearchResultsForm selectFilter(RightFilter filter) {
    return selectFilterCountChecking(filter, true);

    }
    
    public SearchResultsForm selectFilterByText(
            RightFilter filter, 
            String item) {
        info("Filtering by " + filter.toString().toLowerCase());
        ElementList filterList = null;
        Button filterSeeMore = null;
        switch (filter) {
        case AUTHOR:
            filterList = author;
            filterSeeMore = authorSeeMore;
            break;

        case CONTENT_TYPE:
            filterList = contentType;
            filterSeeMore = contentTypeSeeMore;
            break;

        case COURSE:
            filterList = course;
            filterSeeMore = courseSeeMore;
            break;

        case COURSE_GROUP:
            filterList = courseGroup;
            filterSeeMore = courseGroupSeeMore;
            break;

        case INSTITUTION:
            filterList = institution;
            filterSeeMore = institutionSeeMore;
            break;

        case SUBJECT:
            filterList = subject;
            filterSeeMore = subjectSeeMore;
            break;
        }
        if (filterSeeMore.isPresent()) {
            filterSeeMore.clickAndWait();
        }
        filterList.selectListElement(item);
        browser.waitForPageToLoad();
        return new SearchResultsForm();
    }

}
